package com.lanou3g.springboot_demo.service;

import com.lanou3g.springboot_demo.bean.User;
import com.lanou3g.springboot_demo.bean.UserExample;
import com.lanou3g.springboot_demo.mapper.UserMapper;
import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class UserService {

	@Autowired
	private UserMapper userMapper;

	public List<User> queryAllUser() {
		return userMapper.selectByExample(new UserExample());
	}

	public List<User> queryUserByCondition() {

		// 创建查询对象
		UserExample condition = new UserExample();

		DateTime dateTime = new DateTime(new Date());

		// 查询上周周一0点0分0秒到周日23点59分59秒注册的用户
		Date start = dateTime.minusDays(7).withDayOfWeek(1).withHourOfDay(0).withMinuteOfHour(0).withSecondOfMinute(0).toDate();
		Date end = dateTime.minusDays(7).withDayOfWeek(7).withHourOfDay(23).withMinuteOfHour(59).withSecondOfMinute(59).toDate();
		// 创建查询条件组对象(And关系的查询条件组)
		UserExample.Criteria criteria = condition.createCriteria().andCreatetimeBetween(start, end);

		// 并且锁定状态
		criteria.andStatusEqualTo(3);

		// 并且邮箱非空
		criteria.andEmailIsNotNull();

		// Criteria对象是类似于查询条件组的概念，组与组之间可以用and、or关系
		// and关系直接通过example.createCriteria创建
		// or关系通过example.or()创建
		// 下面就是创建了一个or关系的查询条件组
		UserExample.Criteria orCriteria1 = condition.or();
		orCriteria1.andStatusEqualTo(1);


		List<User> userList = userMapper.selectByExample(condition);
		return userList;
	}

}
